package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Goods;
import com.ruoyi.common.core.domain.entity.Order;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.mapper.GoodsMapper;
import com.ruoyi.system.service.IOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2021-11-15
 */
@Controller
@RequestMapping("/system/order")
public class OrderController extends BaseController {
    private String prefix = "system/order";

    @Autowired
    private IOrderService orderService;

    @Autowired
    private GoodsMapper goodsMapper;

    //@RequiresPermissions("system:order:view")
    @GetMapping()
    public String order() {
        return prefix + "/order";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    // @RequiresPermissions("system:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Order order) throws ParseException {
        SysUser currentUser = ShiroUtils.getSysUser();
        String name = currentUser.getLoginName();
        if ("admin".equals(name)) {
            startPage();
            List<Order> list = orderService.selectOrderList(order);
            return getDataTable(list);
        }
        Order order1 = new Order();
        order1.setRole(name);
        List<Order> list1 = orderService.selectOrderList(order1);
        startPage();
        return getDataTable(list1);

    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Order order) throws ParseException {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:order:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Order order) {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Order order = orderService.selectOrderById(id);
        mmap.put("order", order);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:order:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Order order) {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(orderService.deleteOrderByIds(ids));
    }


    @PostMapping("/cancelOrder")
    @ResponseBody
    public AjaxResult cancelOrderByOrderId(Order order) {
        Order order1 = orderService.selectOrderById(order.getId());
        int nums = order1.getOrderNum();
        Long saleNo = order1.getSaleNo();
        Goods goods = goodsMapper.selectGoodsById(saleNo);
        goods.setGoodNum(goods.getGoodNum() + nums);
        goodsMapper.updateGoods(goods);
        int flag = orderService.deleteOrderById(order1.getId());
        if (flag == 1)
            return AjaxResult.success("退单成功");
        else
            return AjaxResult.error("退单失败");
    }
}
